﻿using HybridLab.Workflow.Activities.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HybridLab.Workflow.Activities
{
	public class HybridLabWorkflowDetailsEditor : UITypeEditor
	{
		[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			if (provider != null)
			{
				var labWorkflowDetails = value as HybridLabWorkflowDetails;
				using (HybridLabWizardForm form = new HybridLabWizardForm(provider, labWorkflowDetails))
				{
					if (form != null)
					{
						form.Start();
						if (form.DialogResult == DialogResult.OK)
						{
							value = form.WorkflowDetails;
						}
					}
				}
			}
			return value;
		}

		[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			foreach (Attribute attribute in context.PropertyDescriptor.Attributes)
			{
				if ((attribute is ReadOnlyAttribute) && ((ReadOnlyAttribute)attribute).IsReadOnly)
				{
					return UITypeEditorEditStyle.None;
				}
			}
			return UITypeEditorEditStyle.Modal;
		}
	}
}
